//
//  GlobalMethod+UI.m
//  MexicoLoan
//
//  Created by WXP on 2025/2/24.
//

#import "GlobalMethod+UI.h"

@implementation GlobalMethod (UI)

#pragma mark - 屏幕
// 灵动岛判断
+ (BOOL)deviceIsSmartIslandScreen {
    for (UIScene *scene in [UIApplication sharedApplication].connectedScenes) {
        if ([scene isKindOfClass:[UIWindowScene class]]) {
            UIWindowScene *windowScene = (UIWindowScene *)scene;
            UIWindow *window = windowScene.windows.firstObject;
            if (window.safeAreaInsets.top > 44) {
                return YES;
            }
        }
    }
    return NO;
}
// 状态栏高度
+ (CGFloat)getStatusBarHeight {
    return  (deviceIsBangsScreen ? ([GlobalMethod deviceIsSmartIslandScreen] ? 54.0 : 47.0) : 20.0);
}
// 导航栏高度
+ (CGFloat)getNavgationBarHeight {
    for (UIScene *scene in UIApplication.sharedApplication.connectedScenes) {
        if ([scene isKindOfClass:[UIWindowScene class]]) {
            UIWindowScene *windowScene = (UIWindowScene *)scene;
            UIStatusBarManager *statusBarManager = windowScene.statusBarManager;
            // 获取状态栏的高度
            CGFloat statusBarHeight = statusBarManager.statusBarFrame.size.height;
            return statusBarHeight + 44.0f;
        }
    }
    // 设置默认值
    return 44.0f + (deviceIsBangsScreen ? ([GlobalMethod deviceIsSmartIslandScreen] ? 54.0f : 47.0f) : 20.0f);
}
// 获取底部安全区域高度
+ (CGFloat)getBottomSafeAreaHeight {
    if (@available(iOS 11.0, *)) {
        UIWindow *window = UIApplication.sharedApplication.windows.firstObject;
        return window.safeAreaInsets.bottom;
    }
    return 0;
}
// 获取tabbar高度
+ (CGFloat)getTabbarHeight{
    return 50 + ([GlobalMethod getBottomSafeAreaHeight] == 0 ? LEFT_MARDIN : [GlobalMethod getBottomSafeAreaHeight]);
}

#pragma mark - 宽度-高度
+ (CGFloat)fetchHeightFromLabel:(UILabel *)label{
    return [self fetchHeightFromLabel:label heightLimit:10000];
}
+ (CGFloat)fetchHeightFromLabel:(UILabel *)label heightLimit:(CGFloat )height{
    if (label == nil) {
        return 0;
    }
    NSAttributedString * attributeString = [[NSAttributedString alloc]initWithString:!isStr(label.text)? @"A":label.text attributes:@{NSFontAttributeName: label.font}];
    CGRect rect =[attributeString boundingRectWithSize:CGSizeMake(label.width, height)  options:NSStringDrawingUsesLineFragmentOrigin context:nil];
    CGFloat num_height_return = rect.size.height+1;
    //限制行数
    if (label.numberOfLines != 0) {
        attributeString = [[NSAttributedString alloc]initWithString:@"A" attributes:@{NSFontAttributeName: label.font}];
        rect =[attributeString boundingRectWithSize:CGSizeMake(label.width, height)  options:NSStringDrawingUsesLineFragmentOrigin context:nil];
        num_height_return = num_height_return >label.numberOfLines*rect.size.height?label.numberOfLines*rect.size.height:num_height_return;
    }
    return ceil(num_height_return);
}
+ (CGFloat)fetchWidthFromLabel:(UILabel *)label{
    NSString * strContent = label.text == nil ? @"":label.text;
    UIFont * font = label.font;
    NSAttributedString * attributeString = [[NSAttributedString alloc]initWithString:strContent attributes:@{NSFontAttributeName: font}];
    CGRect rect =[attributeString boundingRectWithSize:CGSizeMake(1000, 1000)  options:NSStringDrawingUsesLineFragmentOrigin context:nil];
    return ceil(rect.size.width);
}
+ (UIView *)getViewWithHeight:(CGFloat)height{
    UIView * view = [UIView new];
    view.height = height;
    return view;
}


#pragma mark - UILabel
+ (void)setLabel:(UILabel *)label widthLimit:(CGFloat )widthLimit fontNum:(CGFloat)fontNum textColor:(UIColor *)textColor text:(NSString *)text{
    [self setLabel:label widthLimit:widthLimit numLines:1 lineSpace:0 isBold:false fontNum:fontNum textColor:textColor aligent:NSTextAlignmentLeft text:text bgColor:[UIColor clearColor]];
}

+ (void)setLabel:(UILabel *)label widthLimit:(CGFloat )widthLimit isBold:(BOOL)isBold fontNum:(CGFloat)fontNum textColor:(UIColor *)textColor text:(NSString *)text{
    [self setLabel:label widthLimit:widthLimit numLines:1 lineSpace:0 isBold:isBold fontNum:fontNum textColor:textColor aligent:NSTextAlignmentLeft text:text bgColor:[UIColor clearColor]];
}

+ (void)setLabel:(UILabel *)label widthLimit:(CGFloat )widthLimit numLines:(NSInteger)numLines
       lineSpace:(CGFloat)lineSpace fontNum:(CGFloat)fontNum textColor:(UIColor *)textColor text:(NSString *)text{
    [self setLabel:label widthLimit:widthLimit numLines:numLines lineSpace:lineSpace isBold:false fontNum:fontNum textColor:textColor aligent:NSTextAlignmentLeft text:text bgColor:[UIColor clearColor]];
}

+ (void)setLabel:(UILabel *)label widthLimit:(CGFloat )widthLimit numLines:(NSInteger)numLines
       lineSpace:(CGFloat)lineSpace fontNum:(CGFloat)fontNum textColor:(UIColor *)textColor aligent:(NSTextAlignment )aligent text:(NSString *)text{
    [self setLabel:label widthLimit:widthLimit numLines:numLines lineSpace:lineSpace isBold:false fontNum:fontNum textColor:textColor aligent:aligent text:text bgColor:[UIColor clearColor]];
}

+ (void)setLabel:(UILabel *)label
      widthLimit:(CGFloat)widthLimit
        numLines:(NSInteger)numLines
       lineSpace:(CGFloat)lineSpace
          isBold:(BOOL)isBold
         fontNum:(CGFloat)fontNum
       textColor:(UIColor *)textColor
         aligent:(NSTextAlignment )aligent
            text:(NSString *)text
         bgColor:(UIColor *)color{
    label.numberOfLines = numLines;
    label.lineSpace = lineSpace;
    if (isBold) {
        label.font = [UIFont boldSystemFontOfSize:fontNum];
    }else{
        label.font = [UIFont systemFontOfSize:fontNum];
    }
    label.textColor = textColor == nil? COLOR_BLACK : textColor;
    label.textAlignment = aligent;
    label.backgroundColor = color == nil?[UIColor clearColor]:color;
    label.text = readStr(text);
    CGFloat widthMAX= [self fetchWidthFromLabel:label];
    if (widthLimit != 0 ) {
        if (widthMAX < widthLimit) {
            label.width = widthMAX;
        }else {
            label.width = widthLimit;
        }
    }else {
        label.width = widthMAX;
    }
    label.height = [self fetchHeightFromLabel:label];
}


#pragma mark - 圆角
+ (void)setRoundView:(UIView *)iv color:(UIColor *)color numRound:(CGFloat)numRound width:(CGFloat)width{
    iv.layer.cornerRadius = numRound;
    iv.layer.masksToBounds = YES;
    [iv.layer setBorderWidth:width];
    iv.layer.borderColor = color.CGColor;
}

@end
